# -*- coding: utf-8 -*-
from fixture import user
from fixture.base import GetRedisValue
from api.base_api.base_api import BaseApi
import time

class RegisterApi(BaseApi):
    captcha_hash_url = "http://192.168.1.141:8001/captcha-image"
    verify_code_url = "http://192.168.1.141:8001/send-verify-code"
    register_url = "http://192.168.1.141:8001/register"

    def get_user_captcha_hash(self):
        # 图形验证码

        self.response = self.get(url=self.captcha_hash_url, payload={})
        captcha_hash = self.response.json()['captcha_hash']
        return captcha_hash

    def get_user_verify_code(self, mobile):
        # 短信验证码
        captcha_hash = self.get_user_captcha_hash()
        captcha_code = user.get_response(captcha_hash)

        payload = {
            "mobile": mobile,
            "captcha_code": captcha_code,
            "captcha_hash": captcha_hash
        }
        time.sleep(70)
        self.post(self.verify_code_url, payload)
        user_verify_code =bytes.decode(GetRedisValue().get_redis_verify_code(mobile))
        return user_verify_code

    def register(self, mobile, verify_code, password):
        # 注册
        payload = {
            "mobile": mobile,
            "verify_code": verify_code,
            "password": password
        }
        response = self.post(self.register_url, payload)
        return response
